import 'package:flutter/material.dart';
import 'package:hellow01/modules/home/home_page.dart';
import '../../modules/regester_module/regester.dart';
import '../../modules/home/home_page.dart';
import '../../Constants/constants.dart'; // show Constants;
import '../../Constants/styles/app_style.dart';

class LoginPage extends StatefulWidget {
  @override
  _LoginPageState createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> with WidgetsBindingObserver {
  @override
  void initState() {
    super.initState();

    //监测键盘加载完后，才能让布局滚动
    WidgetsBinding.instance.addObserver(this);
    WidgetsBinding.instance.addPersistentFrameCallback((Duration timeStamp) {
      if (!_didChangeMetrics) {
        return;
      }
      _preBottomInset = _bottomInset;
      _bottomInset = MediaQuery.of(context).viewInsets.bottom;
      if (_preBottomInset != _bottomInset) {
        WidgetsBinding.instance.scheduleFrame();
        //print(_bottomInset);
        return;
      }

      _didChangeMetrics = false;
      bottomInsertComplete();
    });
  }

  bottomInsertComplete() {
    _croCtl.jumpTo(_croCtl.position.maxScrollExtent);
  }

  @override
  void dispose() {
    _pwdFn.dispose();
    controller.dispose();
    _croCtl.dispose();
    super.dispose();
  }

  bool _didChangeMetrics = false;
  double _bottomInset = 0;
  double _preBottomInset = 0;

  @override
  void didChangeMetrics() {
    super.didChangeMetrics();
    _didChangeMetrics = true;
  }

//密码布局
  Row _layoutPwdGet() {
    return new Row(
      crossAxisAlignment: CrossAxisAlignment.center,
      children: <Widget>[
        SizedBox(width: Constants.Wh * 68),
        Text('密码', style: TextStyleNw.inputBefore),
        Expanded(
          child: TextField(
            controller: controller,
            focusNode: _pwdFn,
            decoration: InputDecoration(
                border: InputBorder.none,
                hintText: '请填写密码',
                contentPadding: EdgeInsets.only(left: Constants.Wh * 180)),
            style: TextFieldStyleNw.input,
            onChanged: (value) {
              setState(() {
                if (value == '' || value == null) {
                  _btnColor = Color(AppColors.DisableBgColor);
                  _btnTxtColor = Color(AppColors.TextGrayTwoColor);
                } else {
                  _btnColor = Color(AppColors.ActivedBgColor);
                  _btnTxtColor = Color(AppColors.TextWhiteColor);
                }
              });
            },
          ),
        ),
        SizedBox(width: Constants.Wh * 68),
      ],
    );
  }

  var _btnColor = Color(AppColors.DisableBgColor);
  var _btnTxtColor = Color(AppColors.TextGrayTwoColor);
  TextEditingController controller = TextEditingController();
  ScrollController _croCtl = ScrollController();
  FocusNode _pwdFn = FocusNode();

  @override
  Widget build(BuildContext context) {
    if (Constants.Wh == null) {
      Constants.Wh = (MediaQuery.of(context).size.width) / 1080;
      Constants.Hh = (MediaQuery.of(context).size.height) / 1920;
    }

    return Scaffold(
      body: SafeArea(
        child: Column(
          children: [
            Expanded(
              child: SingleChildScrollView(
                controller: _croCtl,
                child: Column(children: [
                  SizedBox(height: Constants.Hh * 247),
                  Image.asset('assets/images/ima_user.jpeg',
                      width: Constants.Wh * 216, height: Constants.Wh * 216),
                  SizedBox(height: Constants.Hh * 24),
                  Text(
                    '666666666',
                    textAlign: TextAlign.center,
                    style: TextStyle(fontSize: Constants.Hh * 50),
                  ),
                  SizedBox(height: Constants.Hh * 120),
                  _layoutPwdGet(),
                  SizedBox(height: Constants.Hh * 16),

                  Container(
                      height: 1.0,
                      margin: EdgeInsets.only(
                          left: Constants.Wh * 44, right: Constants.Wh * 44),
                      color: Color(AppColors.MainColor)),
                  SizedBox(height: Constants.Hh * 150),

                  //按钮
                  Container(
                      margin: EdgeInsets.only(
                          left: Constants.Wh * 44, right: Constants.Wh * 44),
                      child: MaterialButton(
                        minWidth: double.infinity,
                        color: _btnColor,
                        highlightColor: Color(AppColors.PressedBgColor),
                        elevation: 0,
                        padding: EdgeInsets.only(
                            top: Constants.Hh * 30, bottom: Constants.Hh * 30),
                        shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.circular(5)),
                        child: Text('登录',
                            style: TextStyle(
                                fontSize: Constants.Wh * 44,
                                color: _btnTxtColor)),
                        onPressed: () {
                          if(controller.text.trim() == '' || controller.text.trim() == null)
                          {}
                          else{
                            //Navigator.of(context).popAndPushNamed('/homePage');
                            Navigator.of(context).pushNamed('/homePage');
                          }
                        },
                      )),
                ]),
              ),
            ),

            //最下面2个按钮
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                MaterialButton(
                    child: Text('找回密码',
                        style: TextStyle(fontSize: Constants.Wh * 38)),
                    onPressed: () {}),
                MaterialButton(
                    child: Text('注册',
                        style: TextStyle(fontSize: Constants.Wh * 38)),
                    onPressed: () {
                      Navigator.of(context).push(
                          MaterialPageRoute(builder: (BuildContext context) {
                        return RegesterPage();
                      }));
                    }),
              ],
            )
          ],
        ),
      ),
    );
  }
}
